package com.soufang.soufangdemo.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class CustomizeErrorController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomizeErrorController.class);

    @ExceptionHandler(Exception.class)
    public ApiResponse<Object> handleException(Exception e) {
        LOGGER.error("handle global exception: " + e);
        return ApiResponse.error(Status.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(BusinessException.class)
    public ApiResponse<Object> handleBusinessException(BusinessException e) {
        LOGGER.debug("业务异常 (code=" + e.getCode() + "):", e);
        return ApiResponse.error(e.getCode(), e.getMessage());
    }
}
